package com.itshizhan.itjidancideveloper.service.impl;

import com.itshizhan.itjidancideveloper.constants.Constants;
import com.itshizhan.itjidancideveloper.dao.InviteCodeDao;
import com.itshizhan.itjidancideveloper.dao.UserDao;
import com.itshizhan.itjidancideveloper.models.InviteCode;
import com.itshizhan.itjidancideveloper.models.User;
import com.itshizhan.itjidancideveloper.models.vo.UserVO;
import com.itshizhan.itjidancideveloper.response.Response;
import com.itshizhan.itjidancideveloper.service.UserService;
import com.itshizhan.itjidancideveloper.utils.JwtUtils;
import com.itshizhan.itjidancideveloper.utils.SfIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserDao userDao;
	@Autowired
	InviteCodeDao inviteCodeDao;
	@Autowired
	SfIdWorker sfIdWorker;
	@Autowired
	BCryptPasswordEncoder bCryptPasswordEncoder;

	@Override
	public Response<UserVO> register(User user, String inviteCode) {
		//检查邮箱是否已经注册
		User dbUser = userDao.findOneByAccount(user.getAccount());
		if(dbUser!=null){
			return Response.fail("当前账号已经注册");
		}
		//检查邀请码是否存在并且有效
		InviteCode oneByCodeAndValid = inviteCodeDao.findOneByCodeAndValid(inviteCode, 1);
		if(oneByCodeAndValid==null){
			return Response.fail("邀请码无效，请关注左侧公众号获取注册邀请码");
		}
		//生成注册时的用户ID
		Long id = sfIdWorker.nextId();
		user.setId(id);
		String pwd = user.getPassword();
		user.setPassword(bCryptPasswordEncoder.encode(pwd));
		//默认注册为普通用户，未激活邮箱
		user.setRoleId(1);
		user.setValid(0);
		user.setEmail(user.getAccount());
		User save = userDao.save(user);
		UserVO userVo = getUserVO(save);
		return Response.success(userVo);
	}

	@Override
	public Response<Map<String,Object>> login(User user) {
		//用户是否注册
		String account = user.getAccount();
		String password = user.getPassword();
		User dbUser = userDao.findOneByAccount(account);
		if(dbUser==null){
			//没有注册
			return Response.fail("用户或密码错误");
		}
		//用户已注册，对比密码
		boolean matches = bCryptPasswordEncoder.matches(password, dbUser.getPassword());
		if (!matches) {
			return Response.fail("用户或密码错误");
		}
		//判断用户状态，如果是非正常的状态，则返回结果
		int valid = dbUser.getValid();
		if (valid==2){
			return Response.fail("当前用户禁止登录");
		}
		//登录成功，返回token和userInfo
		String token = createToken(dbUser);
		Map<String,Object> userInfo = new HashMap<>();
		UserVO userVo = getUserVO(dbUser);
		userInfo.put("token",token);
		userInfo.put("userInfo",userVo);
		return Response.success(userInfo);

	}

	private String createToken(User dbUser) {
		Map<String,Object> claims = new HashMap<>();
		claims.put(Constants.User.Id,dbUser.getId());
		claims.put(Constants.User.Account,dbUser.getAccount());
		claims.put(Constants.User.NickName,dbUser.getNickName());
		claims.put(Constants.User.RoleId,dbUser.getRoleId());
		claims.put(Constants.User.Valid,dbUser.getValid());
		return JwtUtils.createJWT(claims);
	}

	private UserVO getUserVO(User dbUser) {
		UserVO userVo = new UserVO();
		BeanUtils.copyProperties(dbUser, userVo);
		return userVo;
	}

	public static void main(String[] args) {
		System.out.println("test pwd:" +  new BCryptPasswordEncoder().encode("li12345678"));
	}
}
